package cn.cau.epidemicsurveysystem.control;


import cn.cau.epidemicsurveysystem.bean.EpidMissionRecordBean;
import cn.cau.epidemicsurveysystem.bean.EpidTemplateBean;
import cn.cau.epidemicsurveysystem.bean.QuestionAnsBean;
import cn.cau.epidemicsurveysystem.bean.StaffInfoBean;
import cn.cau.epidemicsurveysystem.service.EpidMissionRecordService;
import cn.cau.epidemicsurveysystem.service.StaffInfoService;
import cn.cau.epidemicsurveysystem.service.TemplateService;
import cn.cau.epidemicsurveysystem.vo.TemplateAnsVO;
import cn.cau.epidemicsurveysystem.vo.TemplateVO;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.data.domain.Sort;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.sql.Date;
import java.util.List;

/**
 * @projectName: gp2022
 * @package: cn.cau.epidemicsurveysystem.control
 * @className: ReportController
 * @author: zfy
 * @description: 所有流调任务相关跳转及数据处理
 * @date: 2022/2/27 0003 15:22
 * @version: 1.0
 */
@Controller
public class EpidMissionController {

    @Autowired
    EpidMissionRecordService epidMissionRecordService;
    @Autowired
    TemplateService templateService;
    @Autowired
    StaffInfoService staffInfoService;

    /**
     * @param :binder
     * @return
     * @author zfy
     * @description 将前端Date类型修改为java.sql.Date类型
     * @date 2022/3/1 16:45
     */
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
       //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
          binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    /**
     * @param :
     * @return ModelAndView
     * @author zfy
     * @description 流调任务列表
     * @date 2022/2/27 16:45
     */
    @RequestMapping(value = "/epidMissionList")
    public ModelAndView epidMissionList(){
        ModelAndView modelAndView = new ModelAndView();
        Sort sort = Sort.by(Sort.Direction.DESC, "missionId");
        List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findAll(sort);
//        for (EpidMissionRecordBean e : epidMissionRecordBeans) {
//            System.out.println(e.getMissionId());
//            System.out.println(e.getMissionName());
//        }
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_missionrecord");
        return  modelAndView;
    }
    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description 任务详情页
     * @date 2022/2/28 9:45
     */
    @RequestMapping(value = "/epidMissionDetails")
    public ModelAndView epidMissionDetails(@RequestParam("missionId") int missionId){
        ModelAndView modelAndView = new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        modelAndView.addObject("epidMissionRecordBean",epidMissionRecordBean);
        modelAndView.setViewName("epid_recorddetail");
        return modelAndView;
    }

    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description 任务发布页面
     * @date 2022/3/1 16:45
     */
    @RequestMapping(value = "/epidRecordManage")
    public ModelAndView epidRecordManage(@RequestParam("missionId") int missionId){
        ModelAndView modelAndView=new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        List<EpidTemplateBean> epidTemplateBeans=templateService.getAllTemplate();
        List<StaffInfoBean> staffInfoBeans=staffInfoService.findStaffInfoBeansByStaffWorkStateAndStaffWorkAreaAndStaffPositionOrderByStaffNo(0,epidMissionRecordBean.getAssignPlace(),"流调人员");
        modelAndView.addObject("epidMissionRecordBean",epidMissionRecordBean);
        modelAndView.addObject("epidTemplateBeans",epidTemplateBeans);
        modelAndView.addObject("staffInfoBeans",staffInfoBeans);
        modelAndView.setViewName("epid_recordmanage");
        return  modelAndView;
    }

    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description  二次确认
     * @date 2022/3/1 12:45
     */
    @RequestMapping(value = "/epidRecordDoubleCheck")
    public ModelAndView epidRecordDoubleCheck(@RequestParam("missionId") int missionId){
        ModelAndView modelAndView=new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        List<StaffInfoBean> staffInfoBeans=staffInfoService.findStaffInfoBeansByStaffWorkStateAndStaffWorkAreaAndStaffPositionOrderByStaffNo(0,epidMissionRecordBean.getAssignPlace(),"医生");

        modelAndView.addObject("epidMissionRecordBean",epidMissionRecordBean);
        modelAndView.addObject("staffInfoBeans",staffInfoBeans);
        modelAndView.setViewName("epid_recorddoublecheck");
        return modelAndView;
    }
    /**
     * @param :missionId，templateId，staffId，time1，response，session
     * @return ModelAndView
     * @author zfy
     * @description 任务发布
     * @date 2022/3/1 16:45
     */
    @RequestMapping(value = "/epidMissionPublish" ,method = RequestMethod.POST)
    public ModelAndView epidMissionPublish(@RequestParam("missionId") int missionId, @RequestParam("templateId") int templateId, @RequestParam("staffId") int staffId, @RequestParam("time1") Date time1, HttpServletResponse response, HttpSession session)throws IOException{
        ModelAndView modelAndView=new ModelAndView();

        StaffInfoBean staffInfoBean=staffInfoService.findStaffById(staffId);
//        staffInfoBean.setStaffWorkState(0);
//        staffInfoService.save(staffInfoBean);
        StaffInfoBean staffInfoBean1=(StaffInfoBean)session.getAttribute("staffInfo");

//        查找并修改任务
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        epidMissionRecordBean.setMissionState(1);
        epidMissionRecordBean.setMissionPublishTime(new Date(System.currentTimeMillis()));
        epidMissionRecordBean.setEpidTemplateId(templateId);
        epidMissionRecordBean.setMissionPublisherId(staffInfoBean1.getStaffId());
        epidMissionRecordBean.setMissionPublisherName(staffInfoBean1.getStaffName());
        epidMissionRecordBean.setAssignTime(time1);
        epidMissionRecordBean.setAssigneeId(staffId);
        epidMissionRecordBean.setAssigneeName(staffInfoBean.getStaffName());
        epidMissionRecordService.save(epidMissionRecordBean);

//        返回任务列表
        Sort sort = Sort.by(Sort.Direction.DESC, "missionId");
        List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findAll(sort);
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_missionrecord");

        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('任务已发布');");
        out.println("</script>");

        return  modelAndView;
    }


    /**
     * @param :missionId，secondQuestion，time1，staffId，response
     * @return ModelAndView
     * @author zfy
     * @description 二次发布
     * @date 2022/3/1 14:42
     */
    @RequestMapping(value = "/epidMissionSecondPublish", method = RequestMethod.POST)
    public ModelAndView epidMissionSecondPublish(@RequestParam("missionId") int missionId, @RequestParam("secondQuestion") String secondQuestion,@RequestParam("time1") Date time1, @RequestParam("staffId") int staffId,HttpServletResponse response)throws IOException{
        ModelAndView modelAndView=new ModelAndView();

        StaffInfoBean staffInfoBean=staffInfoService.findStaffById(staffId);

        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        epidMissionRecordBean.setMissionState(5);
        epidMissionRecordBean.setSecondQuestion(secondQuestion);
        epidMissionRecordBean.setSecondPublishTime(time1);
        epidMissionRecordBean.setSecondAssigneeId(staffId);
        epidMissionRecordBean.setSecondAssigneeName(staffInfoBean.getStaffName());
        epidMissionRecordService.save(epidMissionRecordBean);

        Sort sort = Sort.by(Sort.Direction.DESC, "missionId");
        List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findAll(sort);
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_missionrecord");

        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('任务已发布');");
        out.println("</script>");
        return  modelAndView;
    }


    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description  任务取消
     * @date 2022/3/2 8:45
     */
    @RequestMapping(value = "/epidMissionCancel")
    public ModelAndView epidMissionCancel(@RequestParam("missionId")int missionId,HttpServletResponse response)throws IOException{
        ModelAndView modelAndView=new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        epidMissionRecordBean.setMissionState(3);
        epidMissionRecordService.save(epidMissionRecordBean);


        Sort sort = Sort.by(Sort.Direction.DESC, "missionId");
        List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findAll(sort);
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_missionrecord");

        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('任务已取消');");
        out.println("</script>");
        return  modelAndView;
    }

    /**
     * @return ModelAndView
     * @param :missionId
     * @author zfy
     * @description  任务完成
     * @date 2022/3/2 10：12
     */
    @RequestMapping(value = "/epidMissionFinish")
    public ModelAndView epidMissionFinish(@RequestParam("missionId")int missionId, HttpServletResponse response) throws IOException {
        ModelAndView modelAndView=new ModelAndView();

        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
        epidMissionRecordBean.setMissionState(4);
        epidMissionRecordService.save(epidMissionRecordBean);


        Sort sort = Sort.by(Sort.Direction.DESC, "missionId");
        List<EpidMissionRecordBean> epidMissionRecordBeans=epidMissionRecordService.findAll(sort);
        modelAndView.addObject("epidMissionRecordBeans",epidMissionRecordBeans);
        modelAndView.setViewName("epid_missionrecord");

        response.setContentType("text/html; charset=utf-8");
        PrintWriter out=response.getWriter();
        out.println("<script language='javascript'>");
        out.println("alert('任务已完成');");
        out.println("</script>");

        return  modelAndView;
    }

    /**
     * @param :missionId
     * @return ModelAndView
     * @author zfy
     * @description  查看流调问卷答案
     * @date 2022/3/1 16:45
     */
    @RequestMapping(value = "/epidTemplateCheck")
    public ModelAndView epidTemplateCheck(@RequestParam("missionId")int missionId){
        ModelAndView modelAndView = new ModelAndView();
        EpidMissionRecordBean epidMissionRecordBean=epidMissionRecordService.findEpidMissionRecordBeanByMissionId(missionId);
//        System.out.println("aaa");
//        TemplateVO templateVO = templateService.getTemplateVOById(epidMissionRecordBean.getEpidTemplateId());
//        System.out.println("bbb");
        TemplateAnsVO templateAnsVO=templateService. getTemplateAnsVOByMissionId(missionId);
//        System.out.println("ccc");
//        modelAndView.addObject("template",templateVO);
        modelAndView.addObject("templateAnsVO",templateAnsVO);
        modelAndView.addObject("missionId",missionId);
        modelAndView.setViewName("epid_template");
        return modelAndView;
    }
}
